package jok.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {

	public static void main(String[] args) {

		DatagramSocket ds = null;
		BufferedReader br = null;
		try {
			// 申明客户端Socket
			ds = new DatagramSocket();
			// 指定申明服务端IP地址
			InetAddress ia = InetAddress.getByName("192.168.1.20");
			// 持续发送
			while (true) {
				// 读取控制台输入
				br = new BufferedReader(new InputStreamReader(System.in));
				String line = null;
				if ((line = br.readLine()) != null) {
					byte[] bs = line.getBytes();
					// 组装要发送的数据包
					DatagramPacket dp = new DatagramPacket(bs, bs.length, ia, 10000);
					// 发送数据包
					ds.send(dp);
					// 定义退出
					if ("exit".equals(line)) {
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ds != null)
					ds.close();
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
